//复用头部
define(["jquery"],function($){
	/*面向对象*/
	function Header(){
		this.load();
	};
	Header.prototype = {
		constructor:Header,
		//加载头部尾部
		load:function(){
			//头部			
			$.get("/html/include/header.html",$.proxy(this.headerHandler,this));
			$("footer").load("/html/include/footer.html");
		},
		//处理头部加载完成后的任务
		headerHandler:function(data){
			$("header").html(data);
			this.addListener();
		},
		//注册事件监听
		addListener:function(){
			//搜索框
			$(".search>input:first").keyup(this.searchHander);
			//提示框
			$(".suggest").on("click","div",this.suggestHander);
			//nav
			$(".color1,.color2").mouseenter(this.navEnter);
			$(".color1,.color2").mouseleave(this.navLeave);
			$(".megapanel").mouseenter(function() {
				$(this).stop().fadeIn();
			});
			$(".megapanel").mouseleave(function() {
				$(this).fadeOut();
			});
		},
		//鼠标移入导航
		navEnter: function() {
			$(this).next().fadeIn();
		},
		// 鼠标移出导航
		navLeave: function() {
			$(this).next().fadeOut();
		},
		searchHander:function(){
			var word = $(this).val();
			var url =`https://suggest.taobao.com/sug?code=utf-8&q=${word}&callback=?`;
			$.getJSON(url,function(data){
//					console.log(data);
				var html = "";
				data.result.forEach(function(curr){
					html += `<div style="cursor: pointer;">${curr[0]}</div>`;
				});
				$(".suggest").html(html); 
			});
		},
		suggestHander:function(){
			$(".search>input:first").val(this.innerText);
			$(".suggest").empty();
		}
	};
	new Header();
});
	
	
	
	
	
	
/*	
	//加载头部
	//$("header").load("/html/include/header.html");
	//$.get("/html/include/header.html",function(data){
	//	$("header").html(data);
	//});
	//面向过程
	$.ajax({
		type:"get",
		url:"/html/include/header.html",
		success:function(data){
			$("header").html(data);
			//为搜索框绑定按键事件
			$(".search>input:first").keyup(function(){
//				console.log(this.value);
				var word = $(this).val();
				var url =`https://suggest.taobao.com/sug?code=utf-8&q=${word}&callback=?`;
				$.getJSON(url,function(data){
//					console.log(data);
					var html = "";
					data.result.forEach(function(curr){
						html += `<div style="cursor: pointer;">${curr[0]}</div>`;
					});
					$(".suggest").html(html); 
				});
			});
			//为提示框每个div绑定点击事件
			$(".suggest").delegate("div","click",function(){
//				console.log(this.innerText);
				$(".search>input:first").val(this.innerText);
				$(".suggest").empty();
			});
		}
	});
	
	//加载尾部
	$("footer").load("/html/include/footer.html");*/
